<template>
    <div class="container">

      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 卡片 -->
      <el-card class="box-card">
        <!-- 顶部搜索部分 -->
        <el-row :gutter="8">
          <!-- 索索狂 -->
          <el-col :span="8">
            <el-input placeholder="请输入关键字" v-model="keywords">
              <!-- 点击进行关键字搜索 -->
              <el-button slot="append" icon="el-icon-search" @click="getUserData"></el-button>
            </el-input>
          </el-col>

          <!-- 按钮 -->
          <el-col :span="3">
            <el-button @click="userDialogVisible=!userDialogVisible" type="primary">添加用户</el-button>
          </el-col>
        </el-row>

        <!-- 主体部分--表格 -->
        <el-table :border="true" :data="userList" style="width: 100%">

          <el-table-column type="index" width="50">
          </el-table-column>

          <el-table-column prop="username" label="姓名" width="80">
          </el-table-column>

          <el-table-column prop="email" label="邮箱">
          </el-table-column>

          <el-table-column prop="mobile" label="电话">
          </el-table-column>

          <el-table-column prop="role_name" label="角色">
          </el-table-column>

          <el-table-column prop="" label="用户状态">
            <!-- 创建作用域插槽，需要访问组件中的数据 -->
            <template slot-scope="scope">
              <el-switch v-model="scope.row.mg_state" @change="changeStatus(scope.row)"></el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="" label="操作" width="220">
            <!-- 按钮操作，插槽作用域 需要把组件的数据暴露 -->
            <template slot-scope="scope">

            <!-- 编辑按钮 -->
            <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
                <el-button @click="showEditDialog(scope)" size="mini" type="primary" icon="el-icon-edit"></el-button>
            </el-tooltip>

            <!-- 删除按钮 -->
            <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
                <el-button @click="removeUser(scope)" size="mini" type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>

            <!-- 分配角色 -->
            <el-tooltip class="item" effect="dark" content="分配角色" placement="top-start">
              <!-- 点击按钮，展示数据 -->
                <el-button @click="showSetRoleDialog(scope)" size="mini" type="warning" icon="el-icon-setting"></el-button>
            </el-tooltip>

            </template>
        </el-table-column>
        </el-table>

        <!-- 分页功能 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

        <!-- addUser对话框。。。。。。。。。。。。。。 -->
        <el-dialog
          title="添加新用户"
          :close="resetAddUserFrom"
          :visible.sync="userDialogVisible">

          <!-- 添加用户表单 -->
          <el-form
          :model="addUserForm"
          :rules="addUserFormRules"
          ref="addUserFormRef"
          label-width="70px">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="addUserForm.username"></el-input>
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input v-model="addUserForm.password"></el-input>
            </el-form-item>

            <el-form-item label="邮箱" prop="email">
              <el-input v-model="addUserForm.email"></el-input>
            </el-form-item>

            <el-form-item label="电话" prop="mobile">
              <el-input v-model="addUserForm.mobile"></el-input>
            </el-form-item>
          </el-form>

          <!-- 取消，确定按钮 -->
          <span slot="footer" class="dialog-footer">
            <el-button @click="userDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addUser">确 定</el-button>
          </span>
        </el-dialog>

        <!-- 编辑用户对话框 .....................-->
        <el-dialog
          title="编辑用户信息"
          :visible.sync="editDialogVisible">

          <!-- 编辑用户表单 -->
          <el-form
            label-width="55px"
            :model="editUserForm"
            :rules="editUserFormRules"
            ref="editRuleFormRef">

            <el-form-item label="名称" prop="username">
              <el-input :disabled="true" v-model="editUserForm.username"></el-input>
            </el-form-item>

            <el-form-item label="邮箱" prop="email">
              <el-input v-model="editUserForm.email"></el-input>
            </el-form-item>

            <el-form-item label="电话" prop="mobile">
              <el-input v-model="editUserForm.mobile"></el-input>
            </el-form-item>

          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="eaitUser">确 定</el-button>
          </span>
        </el-dialog>

        <!-- 分配用户权限、、对话框 .....................-->
      <el-dialog
        title="分配角色"
        :visible.sync="setRoleDialogVisible"
        width="50%">
        <!-- Start：分配角色 -->
        <el-form ref="setRoleFormRef" :model="setRoleForm" label-width="100px">
          <el-form-item label="当前的用户：">
            <span>{{setRoleForm.username}}</span>
          </el-form-item>

          <el-form-item label="当前的角色：">
            <span>{{setRoleForm.role_name}}</span>
          </el-form-item>

          <el-form-item label="分配新角色：">
            <el-select v-model="setRoleForm.newRoleId" placeholder="请选择">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <!-- Ended：分配角色 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="setRoleDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="setNewRole">确 定</el-button>
        </span>
      </el-dialog>

      </el-card>
    </div>
  </template>

<script>
// 混入自己的js代码
import mix from './user-mixins'
export default {
  mixins: [mix]
}
</script>

<style>

</style>
